package cn.lanqiao.dao.daoImpl;

import cn.lanqiao.dao.ManagerDao;
import cn.lanqiao.entity.Manager;

import java.sql.*;
import java.util.ArrayList;

public class ManagerDaoImpl implements ManagerDao {
    @Override
    public Manager login(String account, String password) {
        Connection conn = null;
        PreparedStatement prep = null;
        //创建一个集合用于装 Manager对象的值
        ArrayList<Manager> managers = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            //实现连接 shixun--数据库名称
            String url = "jdbc:mysql://localhost:3306/qimolx";
            String user = "root";
            String pwd = "root";
            conn = DriverManager.getConnection(url,user,pwd);
            //定义sql语句
            String sql = "select * from managers where magerAccount=? and magerPassword=?";

            //获取pstmt对象
             prep = conn.prepareStatement(sql);
            //设置参数 参数1--问好的下标  参数2--问好代替的内容
            prep.setString(1,account);
            prep.setString(2,password);
            //执行
            ResultSet res = prep.executeQuery();
            //创建一个集合用于装 Manager对象的值
            managers  = new ArrayList<>();
            while (res.next()){
                //获取数据库信息
                String magerAccount = res.getString("magerAccount");
                int magerPassword = res.getInt("magerPassword");
                String magerName = res.getString("magerName");
                //创建对象
                Manager m = new Manager();
                //赋值
                m.setMagerPassword(String.valueOf(magerPassword));
                m.setMagerAccount(magerAccount);
                m.setMagerName(magerName);
                //将 manager对象放入集合 添加
                managers.add(m);
            }

        }  catch (Exception e) {
            throw new RuntimeException(e);
        }
        return managers.get(0);
    }
}
